package com.itheima.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.entity.CheckgroupCheckitem;
import com.itheima.mapper.CheckgroupitemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class CheckgroupitemServiceImpl
        extends ServiceImpl<CheckgroupitemMapper, CheckgroupCheckitem>
        implements CheckGroupItemService {

    @Autowired
    private CheckgroupitemMapper mapper;

    // 根据检查项id判断是否存在关联关系
    @Override
    public boolean checkItemExisted(Integer checkitemId) {
        LambdaQueryWrapper<CheckgroupCheckitem> wrapper = new LambdaQueryWrapper<CheckgroupCheckitem>();
        wrapper.eq(CheckgroupCheckitem::getCheckitemId,checkitemId);
        Integer integer = mapper.selectCount(wrapper);
        if (integer > 0 ){
            return true;
        }else {
            return false;
        }
    }

    // 根据检查组id删除关联关系
    @Override
    public void removeByGroupId(Integer groupId) {
        //条件构造器
        LambdaUpdateWrapper<CheckgroupCheckitem> weapper = new LambdaUpdateWrapper<>();
        weapper.eq(CheckgroupCheckitem::getCheckgroupId,groupId);
        mapper.delete(weapper);
    }

    //根据检查组id获取关联关系列表
    @Override
    public List<CheckgroupCheckitem> listByGroupId(Integer groupId) {
        QueryWrapper<CheckgroupCheckitem> wrapper = new QueryWrapper<>();
        wrapper.eq("checkgroup_id",groupId);
        List<CheckgroupCheckitem> list = mapper.selectList(wrapper);
        return list;
    }

}
